<?php
	function f101($sql){
		//data handle instance
		//set request
		//define format
		//set format
		//define query string
		//process time
		//process fitler
		$dataHandle = new DataHandle();
		$dataHandle->setRequest("101");

		$format = array(
			"Date" => "datem/d/Y",
			);

		$dataHandle->setFormat($format);

		$dataType = getValue($_GET['dataType']);
		$action = getValue($_GET['action']);

		if ($dataType == "json"){
			$error = $dataHandle->processTime($startTime, $endTime);
			if($error == ""){
				$filter = $dataHandle->processFilters($startTime, $endTime, "mix_Produced_Header", "Date");
				$query = getJSONQuery($startTime, $endTime, $filter);
				print($dataHandle->printJSON($sql, $query));
			}
			else
				print($error);
		}
		else{
			$temp = array();
			$temp['error'] = "101: Bad request";
			print(json_encode($temp));
		}

		unset($dataHandle);

	}

	function getJSONQuery(&$startTime, &$endTime, &$filter){

		$queryString = "
				SELECT      
					  mix_Produced_Header.date AS Date,
					  mix_Produced_Header.shift AS Shift, 
					  mix_Produced_Header.number_working AS Crew,
					  mix_Produced_Header.working_minutes_per_day AS MinPerDay,
					  mix_Produced_Header.exception_minutes AS Exception,
					  mix_Produced_Picking_view.picking AS Picking,
					  mix_Produced_Header.notes AS Notes,
					  mix_Produced_Mixer_view.dumping AS Dumping,
					  mix_Produced_Mixer_view.draining AS Draining,
					  mix_Produced_Header.number_working * mix_Produced_Header.working_minutes_per_day as Working,
					  (
					    mix_Produced_Header.number_working 
					    * mix_Produced_Header.working_minutes_per_day
					    - (
					        ISNULL(mix_Produced_Picking_view.picking,0) 
					        + ISNULL(mix_Produced_Mixer_view.dumping,0) 
					        + ISNULL(mix_Produced_Mixer_view.draining,0)
					      )
					  ) AS OverheadTotal,

					  (
					    mix_Produced_Header.number_working 
					    * mix_Produced_Header.working_minutes_per_day
					    - (
					        ISNULL(mix_Produced_Picking_view.picking,0) 
					        + ISNULL(mix_Produced_Mixer_view.dumping,0) 
					        + ISNULL(mix_Produced_Mixer_view.draining,0)
					      )
					  ) 
					  / mix_Produced_Header.number_working AS OverheadPerPerson

					            
					FROM        
					  mix_Produced_Header LEFT OUTER JOIN mix_Produced_Mixer_view 
					    ON mix_Produced_Header.id_shift = mix_Produced_Mixer_view.id_shift 
					  LEFT OUTER JOIN mix_Produced_Picking_view 
					    ON mix_Produced_Header.id_shift = mix_Produced_Picking_view.id_shift ";

		$queryString = $queryString.$filter;

		$queryString = $queryString." order by mix_Produced_Header.Date desc";

		return $queryString;
	}
?>